'''
Created on Nov 12, 2009

@author: Owner
'''
import gamephase.AbstractCheckerGameMode as mBase
import gamemodel.state as mState
import gamemodel.player as mPlayer
import ai.human as mAI
import interface.ConsoleAgent as itfc
import resource_manager

class HumanVsHuman(mBase.AbstractCheckerGameMode):
	'''
	human player vs. human player
	'''
	STAGE_KEY = ['INIT', 'STEP', 'ERROR', 'FINAL']
	STAGE = dict(zip(STAGE_KEY, range(len(STAGE_KEY))))
	def __init__(self):
		'''
		Constructor
		'''
		super(HumanVsHuman, self).__init__()
		self.stage = CN.STAGE['INIT']
		self.state = None
	
	#Methods of base class GamePhase	
	def execute(self):
		if self.stage == CN.STAGE['INIT']:
			self.createGame()
			self.stage = CN.STAGE['STEP']
		elif self.stage == CN.STAGE['STEP']:
			self.tick()
		elif self.stage == CN.STAGE['FINAL']:
			self.end_game()
		pass
	
	#Methods of base class AbstractCheckerGameMode
	def getGameState(self):
		return self.state
	
	def setGameState(self, state):
		self.state = state
	
	#Customized methods
	def createGame(self):
		player0 = mPlayer.CheckerPlayer("Jintao")
		player0.attachAI(mAI.AIHuman)
		player1 = mPlayer.CheckerPlayer("Barack")
		player1.attachAI(mAI.AIHuman)
		state = mState.CheckerState.constructNew(player0, player1)
#		state = resource_manager.ResourceManager\
#				.constructStateMap('state_error', player0, player1)
		self.state = state		
	
	def tick(self):
		itfc.ConsoleDescriptor.drawState(self.state)
		self.annouceLastMove()
		itfc.ConsoleDescriptor.annouceState(self.state)
		
		player = self.state.getCurrentPlayer()
		move = player.getNextMove(self.state)
		if move == -1:
			# not switch player, still on this state
			return
		
		if move == None:
			#loose this game
			self.stage = CN.STAGE['FINAL']
			print("Loose")
			return
		
		# proceed state
		self.take_move(move)
		return

	
CN = HumanVsHuman